package com.resto.scm.web.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;

/**
 * Created by bruce on 2017-09-28 18:04
 */
public class SerialNumberUtil {

    public static String getCategoryCode(Integer categoryHierarchy, String categoryCode, String brandId) {
        String code = "";
        switch (categoryHierarchy) {
            case 1:
                code = (String) RedisUtil.get("SCM_CATEGORY_ONE" + brandId);
                if (StringUtils.isBlank(code)) {
                    code = "1";
                } else {
                    code = String.valueOf(Long.parseLong(code) + 1);
                }
                RedisUtil.set("SCM_CATEGORY_ONE" + brandId, code);
                DecimalFormat f1 = new DecimalFormat("0000");
                code = f1.format(Integer.parseInt(code)) + "00000" + "00000";
                break;
            case 2:
                code = (String) RedisUtil.get("SCM_CATEGORY_TWO" + brandId);
                if (StringUtils.isBlank(code)) {
                    code = "1";
                } else {
                    code = String.valueOf(Long.parseLong(code) + 1);
                }
                RedisUtil.set("SCM_CATEGORY_TWO" + brandId, code);
                DecimalFormat f2 = new DecimalFormat("00000");
                code = categoryCode.substring(1, 5) + f2.format(Integer.parseInt(code)) + "00000";
                break;
            case 3:
                code = (String) RedisUtil.get("SCM_CATEGORY_THREE" + brandId);
                if (StringUtils.isBlank(code)) {
                    code = "1";
                } else {
                    code = String.valueOf(Long.parseLong(code) + 1);
                }
                RedisUtil.set("SCM_CATEGORY_THREE" + brandId, code);
                DecimalFormat f3 = new DecimalFormat("00000");
                code = categoryCode.substring(1, 10) + f3.format(Integer.parseInt(code));
                break;
        }
        return categoryHierarchy + code;
    }

    public static void main(String[] args) {
        System.out.println(getCategoryCode(3, "200080000500000", "f48a0a35e0be4dd8aaeb7cf727603958"));
    }

}
